package com.mt.family.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.family.common.Constants;
import com.mt.family.common.Result;
import com.mt.family.entity.FamilyDictEntity;
import com.mt.family.exception.ServiceException;
import com.mt.family.service.FamilyDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2024-06-26 23:26:07
 */
@RestController
@RequestMapping("/familydict")
public class FamilyDictController {
    @Autowired
    private FamilyDictService familyDictService;

    /**
     * @return teacherInfo
     */
    @GetMapping("/getAll")
    public Result<?> getTeaAll(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<FamilyDictEntity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<FamilyDictEntity> wrapper = new QueryWrapper<>();
        IPage<FamilyDictEntity> orderPage = familyDictService.page(page, wrapper);
        return Result.success(orderPage);
    }

    @GetMapping("/getType")
    public Result<?> getType(@RequestParam(defaultValue = "1") String type) {

        QueryWrapper<FamilyDictEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FamilyDictEntity::getType,type);
        List<FamilyDictEntity> list = familyDictService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 增加信息
     */
    @PostMapping("/add")
    public Result<?> addTea(@RequestBody FamilyDictEntity familyDict){
        if (familyDict.getId() != null) {
            boolean isFalse = familyDictService.updateById(familyDict);
            if (isFalse) {
                return Result.success("修改成功");
            }
        }else {
            boolean isFalse = familyDictService.save(familyDict);
            if (isFalse) {
                return Result.success("添加成功");
            }
        }
        throw new ServiceException(Constants.Code_500,"参数错误");
    }
    /**
     * 批量删除
     */
    @PostMapping("/del")
    public Result<?> delClassByIds(@RequestBody List<Integer> teaIds) {

        boolean isTrue = familyDictService.removeByIds(teaIds);
        if (!isTrue) {
            throw  new ServiceException(Constants.Code_500,"批量删除失败");
        }
        return Result.success("批量删除成功");
    }
    /**
     * @param id
     */
    @GetMapping("/del/{id}")
    public Result<?> delById(@PathVariable Integer id){
        boolean removeById = familyDictService.removeById(id);
        if (!removeById) {
            throw new ServiceException(Constants.Code_500,"删除失败");
        }
        return Result.success(removeById);
    }

    /**
     * @param familyDict 修改的内容
     */
    @PutMapping("/update")
    public Result<?> updateClass(@RequestBody FamilyDictEntity familyDict){
        boolean update = familyDictService.updateById(familyDict);
        if (!update) {
            throw new ServiceException(Constants.Code_500,"修改失败");
        }
        return Result.success();
    }

}
